package com.iot.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.iot.model.v20180120.*;
import com.iot.domain.Device;
import com.iot.service.IotService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author wlr
 * @date 2022/1/6 10:42
 */
@Service
public class IotServiceImpl implements IotService {
    @Value("${iotInstanceId}")
    public String iotInstanceId;

    @Value("${productKey}")
    public String productKey;

    @Value("${productSecret}")
    public String productSecret;

    @Value("${accessKeyID}")
    public String accessKeyID;

    @Value("${accessKeySecret}")
    public String accessKeySecret;

    public String queryProduct(){
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyID, accessKeySecret);
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/
        IAcsClient client = new DefaultAcsClient(profile);

        QueryProductRequest request = new QueryProductRequest();
        request.setIotInstanceId(iotInstanceId);
        request.setProductKey(productKey);

        try {
            QueryProductResponse response = client.getAcsResponse(request);
            return new Gson().toJson(response);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            handleException(e);
        }
        return "";

    }

    public IotServiceImpl() {
    }

    /**
     * 查找设备
     * @return
     */
    public String queryDevice(){
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyID, accessKeySecret);
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/
        IAcsClient client = new DefaultAcsClient(profile);

        QueryDeviceRequest request = new QueryDeviceRequest();
        request.setIotInstanceId(iotInstanceId);
        request.setProductKey(productKey);

        try {
            QueryDeviceResponse response = client.getAcsResponse(request);
            return new Gson().toJson(response);
//            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            handleException(e);
        }
        return "";
    }

    /**
     * 注册设备
     * @param deviceName 设备名称
     * @param nickName 设备别名
     * @return
     */
    public String registerDevice(String deviceName,String nickName){
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyID, accessKeySecret);
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/
        IAcsClient client = new DefaultAcsClient(profile);

        RegisterDeviceRequest request = new RegisterDeviceRequest();
        request.setIotInstanceId(iotInstanceId);
        request.setProductKey(productKey);
        request.setDeviceName(deviceName);
        request.setNickname(nickName);

        try {
            RegisterDeviceResponse response = client.getAcsResponse(request);
            return new Gson().toJson(response);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            handleException(e);
        }
        return "";

    }
    public  String deleteDevice(String deviceName){
        DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyID, accessKeySecret);
        /** use STS Token
         DefaultProfile profile = DefaultProfile.getProfile(
         "<your-region-id>",           // The region ID
         "<your-access-key-id>",       // The AccessKey ID of the RAM account
         "<your-access-key-secret>",   // The AccessKey Secret of the RAM account
         "<your-sts-token>");          // STS Token
         **/
        IAcsClient client = new DefaultAcsClient(profile);

        DeleteDeviceRequest request = new DeleteDeviceRequest();
        request.setIotInstanceId(iotInstanceId);
        request.setProductKey(productKey);
        request.setDeviceName(deviceName);

        try {
            DeleteDeviceResponse response = client.getAcsResponse(request);
            return new Gson().toJson(response);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            handleException(e);
        }
        return "";
    }

    /**
     * 处理异常
     * @param e
     */
    public void handleException(ClientException e){
        System.out.println("ErrCode:" + e.getErrCode());
        System.out.println("ErrMsg:" + e.getErrMsg());
        System.out.println("RequestId:" + e.getRequestId());
    }


}
